﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMLTouch.Core.Entities;
using System.Windows;
using System.Collections;
using UMLTouch.Core;
using UMLTouch.Core.UI;
using UMLTouch.Core.Models;

namespace UMLTouch.Domain.ClassDiagram.Items
{
    public class RelationArrowItem : IDiagramItem,ILinkItem
    {

        List<AnchorPlug> _plugs = new List<AnchorPlug>();

        #region IDiagramItem Members

        public IEnumerable<Segment> Segments
        {
            get;
            protected set;
        }

        public IEnumerable<System.Windows.Point> IntersectionPoints
        {
            get;
            protected set;
        }

        public IEnumerable<System.Windows.Ink.Stroke> Strokes
        {
            get;
            protected set;
        }

        public AnchorPlug Start
        {
            get;
            protected set;
        }
        public AnchorPlug End
        {
            get;
            protected set;
        }

        public Point Position { get; set; }

        public Double X { get; set; }
        public Double Y { get; set; }

        public RelationArrowItem(Shape shape)
        {

            Segments = shape.Segments.ToList();
            Strokes = shape.BaseStrokes.ToList();
            IntersectionPoints = shape.Intersections.Select(inter => inter.IntersectionPoint).ToList();

            Position = (from seg in shape.Segments
                       let xMin = seg.Points.OrderBy(p => p.X).First().X
                       let yMin = seg.Points.OrderBy(p => p.Y).First().Y
                       select new Point(xMin, yMin)).First(); 
                       
            
            
            Point end = shape.SubShape.Intersections.First().IntersectionPoint;


            var ezfz = shape.Intersections
                //.Where(inter => inter.Segments.Count >=2)
                .SelectMany(Inter => Inter.Segments).Distinct(EqualityComparer<SegmentIntersection>.Default).ToList();


            var fg = ezfz.Where(segInter => segInter.IntersectionType == IntersectionType.EndIntersection
                              || segInter.IntersectionType == IntersectionType.StartIntersection)
                .GroupBy(segInter => segInter.Segment, sel => sel.IntersectionType)
                .Where(grouping => grouping.Count() == 1).ToList();

            var sdf = (from plop in fg
                       let p = plop.First() == IntersectionType.EndIntersection ? plop.Key.StartPoint : plop.Key.EndPoint
                       let dist = Math.Abs((p - end).Length)
                       orderby dist descending
                       select p).ToList();

          

            var start = sdf.First();

            //Start = new Point(end.X - Position.X, end.Y - Position.Y);
            //End = new Point(start.X - Position.X, start.Y - Position.Y);
            Start =new AnchorPlug(this,start);
            End = new AnchorPlug(this, end);

            _plugs.Add(Start);
            _plugs.Add(End);
       
        }

        #endregion


       

        #region ILinkItem Members

        public IEnumerable<AnchorPlug> Plugs
        {
            get 
            {
                return _plugs;
            }
        }

        #endregion

        #region IDiagramItem Members

        public event EventHandler<DiagramItemMovedEventArgs> Item_Moved;

        #endregion
    }
}
